package com.biz.controller.mebacitivity;

import com.biz.model.Pmodel.base.PbaseUser;
import com.biz.service.MService.mebactivity.MebactivityServiceI;
import com.biz.service.MService.user.UserServiceI;
import com.biz.utils.ConfigUtil;
import com.biz.utils.StringUtil;
import com.framework.controller.BaseController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/***
 * 商户平台
 * @author lzq
 */
@Controller
@RequestMapping(value="/mebactivity")
public class MebActivityController extends BaseController{

	@Resource(name = "mebactivityService")
	private MebactivityServiceI mebactivityService;

	@Resource(name = "userService")
	private UserServiceI userService;


	/**
	 * 跳转活动页面
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/toMebactivityList")
	public ModelAndView toMebactivityList()throws Exception{
		mv.clear();
		List<Map<String,Object>> list=mebactivityService.loadMebactivityList();
		mv.addObject("list",list);
		mv.setViewName("mebactivity/mebactivityList");
		return mv;
	}

	/**
	 * 跳转活动详情页面
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/toMebactivityDetail")
	public ModelAndView toMebactivityDetail(String id)throws Exception{
		mv.clear();
		String userId=String.valueOf(this.getRequest().getSession().getAttribute(ConfigUtil.get("SESSION_USER_ID")));
		String phone=String.valueOf(this.getRequest().getSession().getAttribute(ConfigUtil.get("SESSION_USER_PHONE")));
//		if(StringUtil.isNullOrEmpty(phone)||"null".equals(phone)){
//			String openId = String.valueOf(this.getSession().getAttribute(ConfigUtil.get("SESSION_OPEN_ID")));
//			PbaseUser pbaseUser = userService.queryUserInfoByOpenId(openId);
//			mv.addObject("pbaseUser",pbaseUser);
//			mv.addObject("page_type","0");
//			mv.setViewName("userCenter/editUserInfo");
//		}else {
			//获取活动详情
			Map<String,Object> map=mebactivityService.findDetail(id);
			mv.addObject("map", map);
			//获取该用户是否报名

			int count=mebactivityService.getApplyCount(userId,id);
			//获取评论列表
			List<Map<String,Object>> commentList=mebactivityService.getCommentList(id);
			mv.addObject("commentList",commentList);
			mv.addObject("count",count);
			mv.setViewName("mebactivity/mebactivityDetail");
//		}
		return mv;
	}

	/**
	 * 用户报名
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/toCommentRecord")
	public void toCommentRecord(String id,String state,HttpServletResponse response)throws Exception{
		String msg="";
		String userId=String.valueOf(this.getRequest().getSession().getAttribute(ConfigUtil.get("SESSION_USER_ID")));
		msg=mebactivityService.toCommentRecord(userId,id,state);
		writeJson(msg,response);
	}

	/**
	 * 跳转活动页面
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/toCommentList")
	public ModelAndView toCommentList(String id)throws Exception{
		mv.clear();
		mv.addObject("id",id);
		mv.setViewName("mebactivity/mebComment");
		return mv;
	}

	/**
	 * 用户报名
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/doSaveComment")
	public void doSaveComment(String id,String note,HttpServletResponse response)throws Exception{
		String msg="";
		String userId=String.valueOf(this.getRequest().getSession().getAttribute(ConfigUtil.get("SESSION_USER_ID")));
		msg=mebactivityService.doSaveComment(userId,id,note);
		writeJson(msg,response);
	}

	/**
	 * 检查用户是否填写手机号
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/checkPhone")
	public void checkPhone(HttpServletResponse response)throws Exception{
		String msg="";
		String phone=String.valueOf(this.getRequest().getSession().getAttribute(ConfigUtil.get("SESSION_USER_PHONE")));
		if(StringUtil.isNullOrEmpty(phone)||"null".equals(phone)){
			msg="1";
		}else {
			msg="0";
		}
		writeJson(msg,response);
	}

	/**
	 * 跳转
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/updateInfo")
	public ModelAndView updateInfo(String id)throws Exception{
		mv.clear();
			String openId = String.valueOf(this.getSession().getAttribute(ConfigUtil.get("SESSION_OPEN_ID")));
			PbaseUser pbaseUser = userService.queryUserInfoByOpenId(openId);
			mv.addObject("pbaseUser",pbaseUser);
			mv.addObject("page_type","0");
			mv.addObject("id",id);
			mv.setViewName("userCenter/editUserInfo");
		return mv;
	}


	/**
	 * 跳转活动回顾
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/toReveiw")
	public ModelAndView toReveiw(String id)throws Exception{
		mv.clear();
		Map<String,Object> map=mebactivityService.findDetail(id);

		mv.addObject("mapP",map);
		mv.setViewName("mebactivity/reveiw");
		return mv;
	}


}
